package com.siao.data
{
	import com.siao.utils.string.trim;
	
	/**
	 * XML值对象
	 * @author SiaoLeon
	 */
	public class XmlValue
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _xml:XML;
		
		/**
		 * XML
		 * @return
		 */
		public function get xml():XML
		{
			return this._xml;
		}
		
		/**
		 * 名称
		 * @return
		 */
		public function get name():String
		{
			return this.dict.getValue("name");
		}
		
		/**
		 * 值
		 * @return
		 */
		public function get value():*
		{
			return this.dict.getValue("value");
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		/**
		 * 字典
		 * @default
		 */
		protected var dict:HashMap = new HashMap();
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 * 获取XML配置中的所有属性
		 * 支持的XML格式如下：
		 * <option name="{key}">{value}</option>
		 * <option name="{key}" value="{value}"/>
		 * <{key} value="{value}"/>
		 * <{key}>{value}</{key}>
		 * @param xml XML配置
		 */
		public function XmlValue(xml:XML)
		{
			this._xml = xml;
			
			this.dict.put("name", xml.hasOwnProperty("@name") ? xml.attribute("name") : xml.name());
			this.dict.put("value", xml.hasOwnProperty("@value") ? xml.attribute("value") : trim(xml.children()).replace(/[\r\n]+/g, ""));
			
			for (var i:int = 0; i < xml.attributes().length(); i++)
			{
				var key:String = xml.attributes()[i].name();
				var value:Object = xml.attributes()[i];
				
				if (key == "name" || key == "value")
				{
					continue;
				}
				
				this.dict.put(key, value);
			}
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 字符串格式
		 * @return
		 */
		public function toString():String
		{
			var result:Array = new Array();
			result.push("value=" + this.value);
			
			for each (var key:* in this.dict.keys)
			{
				if (key == "name" || key == "value")
				{
					continue;
				}
				
				result.push(key + "=" + this.dict.getValue(key));
			}
			
			return "[" + this.name + "(" + result.join(",") + ")" + "]";
		}
		
		/**
		 * 获取属性
		 * @param name 属性名称
		 * @return
		 */
		public function attribute(name:String):*
		{
			return this.dict.getValue(name);
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
